// pages/articleDetail/articleDetail.js
const util = require('../../utils/util.js');
// pages/about/about.js
var WxParse = require('../../wxParse/wxParse.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
		/* 文章id */
		articleId:"",
		/* 当前页面是原稿还是解读 */
		judgeType:"",
		/* 文章内容 */
		article:{},
		/* 机器人移动的位置 */
		leftLooks: 280,
		topLooks: 350,
		/* 原稿id */
		parentId:"",
		/* 发文时间 */
		fileDate:"",
		/* 案例详情 */
		case:"",
		/* 距离底部距离 */
		bottom:100,
		/* 收藏code */
		resultCode:0,
		/* 当前详情状态 */
		status:1,
		/* 收藏可点击 */
		clickCollect:true,
		/* 个人信息是否存在 */
		userInfo:"",
		/* 判断用户是不是第一次获取用户信息 */
		judgeInfo:false,
  },
	/**
	* 生命周期函数--监听页面加载
	*/
	onLoad: function (options) {
		this.data.articleId=options.id;
		if(options.parentId){
			this.data.parentId=options.parentId;
		}
		if(options.case){
			this.setData({
				case:options.case
			})
		}
		this.setData({
			judgeType:options.judgeType
		})
		wx.showShareMenu({  
			withShareTicket:true,
			menus: ['shareAppMessage', 'shareTimeline']
		})
	}, 
	/* 微信授权 */
	getUserInfo:function(){
		var _this=this;
		// 获取用户信息
		wx.getSetting({
			success: res => {
				if (res.authSetting['scope.userInfo']) {
					// 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
					wx.getUserInfo({
						success: detail => {
							wx.setStorageSync("nickName",detail.userInfo.nickName);
							wx.login({  
								success: function(res){
									wx.request({
										url: util.url+"/user/decodeUserInfo",
										data: {
											encryptedData:detail.encryptedData,
											iv:detail.iv,
											code:res.code,
										},
										method:"post",
										header: {
											'content-type': 'application/json' 
										},
										success(res) {
											if(res.data.status!=0){
												_this.data.judgeInfo=true;
												wx.setStorageSync("userInfo",JSON.stringify(res.data.userInfo));
												_this.setData({
													userInfo:wx.getStorageSync("userInfo")
												})
												_this.judgeCollect();
												var timer=setTimeout(function(){
													if(_this.data.resultCode!=1){
														_this.tipCollect();
													}
													clearTimeout(timer);
												},1000)
											}else{
												wx.showToast({
													title:"收藏失败"
												})
											}
										},
										fail: res => {
											wx.showToast({
												title:"收藏失败"
											})
										}
									})
								},
								fail: res => {
									wx.showToast({
										title:"收藏失败"
									})
								}
							})
						},
						fail: res => {
							wx.showToast({
								title:"收藏失败"
							})
						}
					})
				}
			},
			fail: res => {
				wx.showToast({
					title:"收藏失败"
				})
			}
		})
	},
	/**
	   * 生命周期函数--监听页面初次渲染完成
	   */
	onReady: function () {
		var _this=this;
		if(!this.data.judgeType&&this.data.case){
			this.setData({
				bottom:0
			})
		}else{
			wx.createSelectorQuery().selectAll('.article-btn').boundingClientRect(function (rect) {
				_this.setData({
					bottom:rect[0].height,
				})
			}).exec();
		}
		this.scanArticalTime();
		this.setData({
			userInfo:wx.getStorageSync("userInfo")
		})
		if(this.data.userInfo){
			this.judgeCollect();
		}
		this.obtainArticalDetail();
	},
	/* 跳转到问题页面 */
	toQuestion:function(){
		wx.navigateTo({
			url:"../questions/questions"
		})
	},
	/* 文章浏览次数 */
	scanArticalTime:function(){
		var _this=this;
		wx.request({
			url: util.url+"/article/addPageView/"+_this.data.articleId,
			header: {
				'content-type': 'application/json' 
			},
			success(res) {
				if(res.data.resultCode==0){
				}else{
					wx.showToast({
						icon:"none",
						title:res.data.resultMsg
					})
				}
			}
		})
	},
	/* 滑动机器人 */
	imagetouchmove:function(e){
		var phoneWidth=wx.getStorageSync("phoneWidth");
		var phoneHeight=wx.getStorageSync("phoneHeight");
		var robotWidth=wx.getStorageSync("robotWidth");
		var robotHeight=wx.getStorageSync("robotHeight");
		e.touches[0].clientX=e.touches[0].clientX<0?0:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY<0?0:e.touches[0].clientY;
		e.touches[0].clientX=e.touches[0].clientX>phoneWidth-robotWidth?phoneWidth-robotWidth:e.touches[0].clientX;
		e.touches[0].clientY=e.touches[0].clientY>phoneHeight-robotHeight?phoneHeight-robotHeight:e.touches[0].clientY;
		
		this.setData({
			leftLooks: e.touches[0].clientX,
			topLooks: e.touches[0].clientY
		})
	},
	wxParseTagATap:function(e){
		wx.navigateTo({
			url: "../"+e.currentTarget.dataset.src,
			/* fail:function(){
				wx.showToast({
					icon:"none",
					title:"页面跳转地址不准确，请重新配置"
				})
			} */
		})
	},
	/* 获取文章详情 */
	obtainArticalDetail:function(){
		var _this=this;
		wx.request({
			url: util.url+"/article/queryArticle",
			method:"post",
			data: {
				id:_this.data.articleId
			},
			header: {
				'content-type': 'application/json' 
			},
			success(res) {
				if(res.data.resultCode==0){
					if(res.data.resultData.articleList.length){
						res.data.resultData.articleList[0].publishAt=res.data.resultData.articleList[0].publishAt?util.formatTime(new Date(res.data.resultData.articleList[0].publishAt)):"";
						res.data.resultData.articleList[0].fileDate=res.data.resultData.articleList[0].fileDate?util.formatTime(new Date(res.data.resultData.articleList[0].fileDate)):"";
						var content=res.data.resultData.articleList[0].content;
						var arr=content.split('">');
						var index=-1;
						var url="";
						
						for(var i=0;i<arr.length;i++){
							index=parseInt(arr[i].indexOf("href"));
							if(index>-1){
								if(arr[i].indexOf("http://")>-1){
									arr[i]=arr[i].replace(/http:\/\//, "");
								}else if(arr[i].indexOf("https://")>-1){
									arr[i]=arr[i].replace(/https:\/\//, "");
								}
							}
						}
						res.data.resultData.articleList[0].content=arr.join('">');
						if(res.data.resultData.articleList[0].status!=0&&res.data.resultData.articleList[0].status!=1){
							_this.setData({
								article:res.data.resultData.articleList[0],
								status:2
							})
							var articles=res.data.resultData.articleList[0].content;
							if(!_this.data.parentId){
								_this.setData({
									parentId:_this.data.article.parentId
								})
							}
							WxParse.wxParse('articles', 'html', articles,_this, 5);
						}else{
							_this.setData({
								article:[],
								status:1
							})
						}
					}else{
						_this.setData({
							article:{}
						})
					}
					
				}else{
					wx.showToast({
						icon:"none",
						title:res.data.resultMsg
					})
				}
			}
		});
	},
	/* 跳转到原稿详情 */
	toManuscript:function(){
		wx.navigateTo({
			url: "../articleDetail/articleDetail?judgeType=unscramble&id="+this.data.parentId
		})
	},
	/* 判断是否为正整数 */
	isPositiveInteger:function (s){//是否为正整数
		var re = /^[0-9]+$/ ;
		return re.test(s)
	}, 
	/* 跳转到解读 */
	toUnscramble:function(){
		/* if(!this.data.clickUnscramble){
			return;
		} */
		if(this.data.status!=0&&this.data.status!=1&&this.data.articleId!=0&&this.isPositiveInteger(this.data.articleId)){
			wx.navigateTo({
				url:"../unscramble/unscramble?parentId="+this.data.articleId
			})
		}else{
			wx.showToast({
				icon:"none",
				title:"操作失败！！！",
			})
		}
		
	},
	/* 判断是否收藏过 */
	judgeCollect:function(){
		if(this.data.articleId!=0&&this.isPositiveInteger(this.data.articleId)){
			var _this=this;
			wx.request({
				url: util.url+"/usi/isCollection",
				method:"post",
				data: {
					"id":0,
					"type":"",
					"articleId":_this.data.articleId,
					"storeBy":JSON.parse(wx.getStorageSync("userInfo")).openId,	
				},
				header: {
					'content-type': 'application/json' 
				},
				success(res) {
					_this.setData({
						resultCode:res.data.resultCode
					})
					if(_this.data.resultCode==1&&_this.data.judgeInfo){
						wx.showToast({
							icon:"none",
							title:"收藏成功",
						})
					}
				},
			})
		}
	},
	/* 收藏提示 */
	tipCollect:function(){
		if(!this.data.clickCollect){
			return;
		}
		this.data.clickCollect=!this.data.clickCollect;
		if(this.data.status!=0&&this.data.status!=1&&this.data.articleId!=0&&this.isPositiveInteger(this.data.articleId)){
			var _this=this;
			var data= {
					"id":0,
					"articleId":_this.data.articleId,
					"storeBy":JSON.parse(wx.getStorageSync("userInfo")).openId,	
					"file_date":this.fileDate,
				};
				if(this.data.article.status==3||this.data.article.status==4){
					data.type=this.data.article.status;
				}
			wx.request({
				url: util.url+"/usi/saveOrUpload",
				method:"post",
				data:data,
				header: {
					'content-type': 'application/json' 
				},
				success(res) {
					_this.data.clickCollect=!_this.data.clickCollect;
					if(res.data.resultCode==0){
						wx.setStorageSync('collect',true);
						wx.showToast({
							icon:"none",
							title:"收藏成功",
						})
						_this.setData({
							resultCode:1
						})
					}else{
						wx.showToast({
							icon:"none",
							title:res.data.resultMsg,
						})
					}
				},
			})
		}else{
			this.data.clickCollect=!this.data.clickCollect;
			wx.showToast({
				icon:"none",
				title:"操作失败！！！",
			})
		}
	},
		/**
   * 用户点击右上角分享朋友
   */
  onShareAppMessage: function () {

  },
	/**
	* 用户点击右上角分享朋友圈
	*/
	onShareTimeline: function () {
	
	 },
})